<!--
 * @Author: zengwj
 * @Date: 2022-07-05 16:00:45
 * @LastEditors: zengwj
 * @LastEditTime: 2022-09-26 16:16:19
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // new Promise((resolve, reject) => {
    //   console.log("promise1")
    //   resolve()
    // }).then(() => {
    //   console.log("then11")
    //   new Promise((resolve, reject) => {
    //     console.log("promise2")
    //     resolve()
    //   }).then(() => {
    //     console.log("then21")
    //   }).then(() => {
    //     console.log("then23")
    //   })
    // }).then(() => {
    //   console.log("then12")
    // })
    // new Promise((resolve, reject) => {
    //   console.log("promise3")
    //   resolve()
    // }).then(() => {
    //   console.log("then31")
    // }).finally(() => {
    //   console.log(11111111)
    // })
    const promiseList = [
      new Promise((resolve) => {
        setTimeout(() => {
          resolve(1)
        }, 300);
      }),
      new Promise((resolve) => {
        setTimeout(() => {
          resolve(2)
        }, 200);
      }),
      new Promise((resolve) => {
        setTimeout(() => {
          resolve(3)
        }, 100);
      })
    ]

    promiseAll(promiseList).then(res => {
      console.log(res)
    });
    function promiseAll(promises) {
      if (!Array.isArray(promises)) {
        throw new Error('promises must be array')
      }
      return new Promise((resolve, reject) => {
        const result = []
        return promises.forEach(p => {
          p.then((data) => {
            result.push(data)
            result.length === promises.length && resolve(result)
          }, (error) => {
            reject([error])
          })
        })
      })
    }
  </script>
</body>

</html>